import { getUserInfo, login, getUserDetailById } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken(state, tokenValue) {
      state.token = tokenValue
      // 本地存储token
      setToken(tokenValue)
    },
    setUserInfo(state, data) {
      state.userInfo = data
    },
    removeUserInfo(state) {
      state.token = ''
      state.userInfo = {}
      removeToken()
    }
  },
  actions: {
    async fetchLogin(store, data) {
      const res = await login(data)
      store.commit('setToken', res)
    },
    async fetchUserInfo(store) {
      const res = await getUserInfo()
      // 获取用户头像
      const res1 = await getUserDetailById(res.userId)
      // 拼接返回数据，统一传入userInfo中存储
      store.commit('setUserInfo', { ...res, ...res1 })
      // console.log(6)
    }
  }
}
